package de.openkanban.shared.dto;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import com.google.gwt.json.client.JSONObject;
import com.smartgwt.client.util.JSON;

import de.openkanban.client.ui.presenter.RequestParameterMap;
import de.openkanban.client.util.JsonConverter;
import de.openkanban.shared.util.CollectionStringTransformer;
import de.openkanban.shared.util.StatusCodes;

/**
 * 
 * @author msa
 * 
 */
public class KanbanDto extends StatusDto implements RequestParameterMap {

	private Long kanbanId;
	private String title;
	private String description;
	private String effort;
	private Boolean pullable;
	private String kanbanUserString;
	private Set<Long> kanbanUserIds = new HashSet<Long>();

	public KanbanDto() {
	}

	public KanbanDto(String json) {
		super(json);
		JSONObject jso = new JSONObject(JSON.decode(json));
		kanbanId = JsonConverter.getLongValueOrNull(jso, "kanbanId");
		title = JsonConverter.getStringValueOrNull(jso, "title");
		description = JsonConverter.getStringValueOrNull(jso, "description");
		effort = JsonConverter.getStringValueOrNull(jso, "effort");
		pullable = JsonConverter.getBooleanValueOrNull(jso, "pullable");
		
		String kanbanUserString = JsonConverter.getStringValueOrNull(jso, "kanbanUserString");
		if(kanbanUserString != null){
			CollectionStringTransformer cst = new CollectionStringTransformer(kanbanUserIds, kanbanUserString);
			kanbanUserIds = (Set<Long>) cst.toCollection();
		}
		
	}

	public KanbanDto(StatusCodes statusCode) {
		this.fehlerKz = statusCode.getStatusCode();
		this.fehlerText = statusCode.getStatusText();
	}

	public Long getKanbanId() {
		return kanbanId;
	}

	public void setKanbanId(Long kanbanId) {
		this.kanbanId = kanbanId;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getEffort() {
		return effort;
	}

	public void setEffort(String effort) {
		this.effort = effort;
	}

	public Boolean getPullable() {
		return pullable;
	}

	public void setPullable(Boolean pullable) {
		this.pullable = pullable;
	}

	public String getHistoryText() {
		return title + ("... " + description + "...");
	}
	
	public String getKanbanUserString() {
		return kanbanUserString;
	}

	public void setKanbanUserString(String kanbanUserString) {
		this.kanbanUserString = kanbanUserString;
	}
	

	public Set<Long> getKanbanUserIds() {
		return kanbanUserIds;
	}

	public void setKanbanUserIds(Set<Long> kanbanUserIds) {
		this.kanbanUserIds = kanbanUserIds;
	}

	@Override
	public Map<String, String> valuesToMap() {
		Map<String, String> kanbanMap = new HashMap<String, String>();
		if (kanbanId != null) {
			kanbanMap.put("kanbanId", kanbanId.toString());
		}
		kanbanMap.put("title", title);
		kanbanMap.put("description", description);
		if (pullable != null) {
			kanbanMap.put("pullable", pullable.toString());
		}
		kanbanMap.put("effort", effort);
		
		if(kanbanUserIds != null && kanbanUserIds.size() > 0){
			CollectionStringTransformer cst = new CollectionStringTransformer(kanbanUserIds);
			kanbanMap.put("kanbanUserString", cst.toString());
		}

		return kanbanMap;
	}

}
